package com.newlink.building.userinfo.utils;

import android.util.Base64;
import android.util.Log;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class Module_UserInfo_AESUtils {

    private static final String AES_ALGORITHM = "AES/ECB/NoPadding";
    //	private static final String CHARSET = "UTF-8";
    private static final String CHARSET = "ISO8859-1";

    public static String encrypt(String data, String key) throws Exception {
        byte[] key1 = hexStringToByteArray(key);
        Log.e("jake", "key1 :: " + key1.length);
        Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
        SecretKeySpec secretKey = new SecretKeySpec(key1, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] ba = hexStringToByteArray2(data);
        byte[] encryptedData = cipher.doFinal(ba);
        return byteArrayToHexString1(encryptedData);
    }

    public static byte[] hexStringToByteArray1(String hexString) {
        int len = hexString.length();
        Log.e("jake", "");
        // 长度为字符串长度的一半加上 8 个零字节
        byte[] data = new byte[len / 2 + 8];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
                    + Character.digit(hexString.charAt(i + 1), 16));
        }
        return data;
    }

    private static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }

    private static byte[] hexStringToByteArray2(String s) {
        int len = s.length();
        byte[] data = new byte[16];
        Log.e("jake", "data.length " + data.length);
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }

//	public static String byteArrayToHexString(byte[] byteArray) {
//		char[] hexChars = "0123456789ABCDEF".toCharArray();
//		StringBuilder result = new StringBuilder();
//		for (byte b : byteArray) {
//			int highNibble = (b & 0xF0) >>> 4;
//			int lowNibble = b & 0x0F;
//			result.append(hexChars[highNibble]);
//			result.append(hexChars[lowNibble]);
//		}
//		return result.toString();
//	}

    public static String decrypt(String encryptedData, String key) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(CHARSET), "AES");
        Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decryptedData = cipher.doFinal(Base64.decode(encryptedData, Base64.DEFAULT));
        return new String(decryptedData, CHARSET);
    }


    private static String byteArrayToHexString1(byte[] bytes) {
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        for (byte b : bytes) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString();
    }

    public static String tetete() throws Exception {
        String keyHex = "9EF57D1FA4C32459B1675AE83C049B18";
        byte[] keyBytes = hexStringToByteArray(keyHex);

        SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");

        Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");

        cipher.init(Cipher.ENCRYPT_MODE, secretKey);

        byte[] data = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

        byte[] encryptedData = cipher.doFinal(data);

        return byteArrayToHexString1(encryptedData);
//		System.out.println("Encrypted Data (hex): " + byteArrayToHexString1(encryptedData));
    }
}